package com.hos.db.create.ill;

import com.hos.db.entity.ill.IllsEntity;
import com.hos.utile.DLog;
import com.hos.utile.DateUtile;
import com.hos.utile.Md5Utile;
import com.hos.utile.StringUtile;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 病历信息
 * Created by guom on 2018/10/15.
 */
public class IllsTable {
    public static String tableName = "pat_ills";
    public static String tableIllId = "ill_id";//病例id
    public static String tablePatId = "pat_id";//患者id
    public static String tableDocId = "doc_id";//医生id
    public static String tableDocName = "doc_name";//医生姓名
    public static String tableCreateId = "create_id";//所有者id
    public static String tableHosName = "hos_name";//医院名称
    public static String tableHosDeptName = "hos_dept_name";//科室名称
    public static String tableHosInNumber = "hos_in_number";//住院号
    public static String tableHosJoinTime = "hos_join_time";//入院时间
    public static String tableHosOutTime = "hos_out_time";//出院时间
    public static String tableIllDiagnose = "ill_diagnose";//病情诊断
    public static String tableCreateTime = "create_time";// 病例创建时间
    public static String tableUpdateTime = "update_time";// 病例修改时间
    //
    public static String careteTable = "create table  "
            + tableName + " ( "
            + tableIllId + "  varchar(200) primary key, "//主键
            + tablePatId + " varchar(200), "
            + tableDocId + " varchar(200), "
            + tableDocName + " varchar(200), "
            + tableCreateId + " varchar(200), "
            + tableHosName + " longText, "
            + tableHosDeptName + " longText, "
            + tableIllDiagnose + " longText, "
            + tableHosInNumber + " varchar(100), "
            + tableHosJoinTime + " varchar(200), "
            + tableHosOutTime + " varchar(200), "
            + tableCreateTime + " datetime, "
            + tableUpdateTime + " datetime "
            + "); ";

    public static String onInsert(JdbcTemplate jdbcTemplate, IllsEntity bean) {
        String sql = "INSERT INTO " + IllsTable.tableName
                + "("
                + IllsTable.tableIllId + ","
                + IllsTable.tablePatId + ","
                + IllsTable.tableDocId + ","
                + IllsTable.tableDocName + ","
                + IllsTable.tableCreateId + ","
                + IllsTable.tableHosName + ","
                + IllsTable.tableHosDeptName + ","
                + IllsTable.tableHosInNumber + ","
                + IllsTable.tableHosJoinTime + ","
                + IllsTable.tableHosOutTime + ","
                + IllsTable.tableIllDiagnose + ","
                + IllsTable.tableCreateTime + ","
                + IllsTable.tableUpdateTime
                + ")"
                + " VALUES (?,?,?,?, ?, ?,?, ?, ?, ?, ?, ?, ?)";
        //
        Date time = new Date();
        String t = DateUtile.getDateFormat(time, DateUtile.DATA_FORMAT_YMD_HMS);
        String illId = Md5Utile.encode(bean.getCreateId() + t);
        Object[] obj = new Object[]{illId, bean.getPatId(), bean.getDocId(), bean.getDocName(),
                bean.getCreateId(), bean.getHosName(), bean.getHosDeptName(), bean.getHosInNumber(),
                bean.getHosJoinTime(), bean.getHosOutTime(), bean.getIllDiagnose(),
                time, time};
        jdbcTemplate.update(sql, obj);
        return illId;
    }

    //删除
    public static boolean onDlete(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "DELETE  FROM " + IllsTable.tableName
                + " WHERE "
                + IllsTable.tableIllId + " = '" + illId + "'";
        int state = jdbcTemplate.update(sql);
        return state > 0;
    }

    //修改
    public static boolean onUpdate(JdbcTemplate jdbcTemplate, String illId, IllsEntity bean) {
        if (bean == null) {
            return true;
        }
        String update = "";
        String value = bean.getHosName();
        if (value!=null) {
            update += " , " + IllsTable.tableHosName + " = '" + value + "'";
        }
        value = bean.getHosDeptName();
        if (value!=null) {
            update += " , " + IllsTable.tableHosDeptName + " = '" + value + "'";
        }
        value = bean.getHosInNumber();
        if (value!=null) {
            update += " , " + IllsTable.tableHosInNumber + " = '" + value + "'";
        }
        value = bean.getHosJoinTime();
        if (value!=null) {
            update += " , " + IllsTable.tableHosJoinTime + " = '" + value + "'";
        }
        value = bean.getHosOutTime();
        if (value!=null) {
            update += " , " + IllsTable.tableHosOutTime + " = '" + value + "'";
        }
        value = bean.getIllDiagnose();
        if (value!=null) {
            update += " , " + IllsTable.tableIllDiagnose + " = '" + value + "'";
        }
        if (update.equals("")) {
            return true;
        }
        Date time = new Date();
        String t = DateUtile.getDateFormat(time, DateUtile.DATA_FORMAT_YMD_HMS);
        //
        update += " , " + IllsTable.tableUpdateTime + " = '" + t + "'";
        update = update.replaceFirst(" , ", "");
        String sql = "UPDATE " + IllsTable.tableName
                + " SET "
                + update
                + " WHERE "
                + IllsTable.tableIllId + " = '" + illId + "'";
        int s = jdbcTemplate.update(sql);
        return s > 0;
    }

    //修改医生信息
    public static void onUpdateDoc(JdbcTemplate jdbcTemplate, String docId, String docName) {
        if (StringUtile.isEmpty(docName)) {
            return;
        }
        String update = IllsTable.tableDocName + " = '" + docName + "'";
        String sql = "UPDATE " + IllsTable.tableName
                + " SET "
                + update
                + " WHERE "
                + IllsTable.tableDocId + " = '" + docId + "'";
        jdbcTemplate.update(sql);
    }

    public static IllsEntity getResults(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "SELECT * FROM " + IllsTable.tableName
                + " WHERE "
                + IllsTable.tableIllId + " = '" + illId + "'";
        IllsEntity entity = new IllsEntity();
        List<IllsEntity> ills = onQuery(jdbcTemplate, sql);
        if (ills != null && ills.size() > 0) {
            entity = ills.get(0);
        }
        return entity;
    }

    /**
     * @param jdbcTemplate
     * @param patId        患者id
     * @param pageNo       页面（>=1）
     * @param pageSize     每页查询数量
     * @param hasNextPage  判断是否有下一页
     */
    public static List<IllsEntity> getResults(JdbcTemplate jdbcTemplate, String patId,
                                              int pageNo, int pageSize, boolean hasNextPage) {
        int next = 0;
        if (hasNextPage) {
            next = 1;
        }
        String sql = "SELECT * FROM " + IllsTable.tableName
                + " WHERE "
                + IllsTable.tablePatId + " = '" + patId + "'"
                + " ORDER BY " + IllsTable.tableUpdateTime
                + " DESC "
                + " limit "
                + (pageNo - 1) * pageSize + " , " + (pageSize + next);

        List<IllsEntity> ills = onQuery(jdbcTemplate, sql);
        if (ills == null) {
            ills = new ArrayList();
        }
        return ills;
    }


    //查询病历
    private static List<IllsEntity> onQuery(JdbcTemplate jdbcTemplate, String sql) {
        List<IllsEntity> ills = null;
        try {
            ills = jdbcTemplate.query(sql,
                    ParameterizedBeanPropertyRowMapper.newInstance(IllsEntity.class));
            DLog.e("查询成功：==========");
        } catch (Exception e) {
            DLog.e("查询失败：" + e.getMessage());
        }
        return ills;
    }

    //查询病历数量
    public static int getSize(JdbcTemplate jdbcTemplate, String key) {
        String sql;
        if (StringUtile.isEmpty(key)) {
            sql = "SELECT count(*) FROM " + IllsTable.tableName;
        } else {
            sql = "SELECT count(*) FROM " + IllsTable.tableName
                    + " WHERE " + IllsTable.tablePatId + " = '" + key + "'";
        }
        int count = jdbcTemplate.queryForInt(sql);
        return count;
    }
}
